﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Business.Entities;

namespace Data.DataBase
{
    public class PlanAdapter : Data.Database.Adapter
    {
        private static List<Plan> _Planes;

        public static List<Plan> Planes
        {
            get
            {
                if (_Planes == null)
                {
                    _Planes = new List<Plan>();
                    Business.Entities.Plan pl;

                    pl = new Plan();
                    pl.Id = 1;
                    pl.IdEspecialidad = 1;
                    pl.Descripcion = "Ing. en Sistemas de Informacion Plan 2008";
                    pl.State = Business.Entities.BusinessEntity.States.Unmodified;

                    _Planes.Add(pl);
                }
                return PlanAdapter._Planes;
            }
        }

        public List<Plan> GetAll()
        {
            return new List<Plan>(Planes);
        }

        public Plan GetOne(int id)
        {
            return Planes.Find(delegate(Plan pl) {return pl.Id == id;});
        }

        public void Delete(int id)
        {
            this.Delete(this.GetOne(id));
        }
        public void Delete(Plan pl) {
            Planes.Remove(pl);
        }

        public void Save(Plan plan)
        {
            switch (plan.State)
            {
                case BusinessEntity.States.New:
                    int nuevoId = 0;
                    // esta busqueda podria reemplazarse por un OrderBy que orden por ID.
                    foreach (Plan pl in Planes)
                    {
                        if(pl.Id > nuevoId){
                            nuevoId = pl.Id;
                        }
                    }
                    plan.Id = nuevoId + 1;
                    Planes.Add(plan);
                    break;
                case BusinessEntity.States.Modified:
                    Planes[Planes.FindIndex(delegate(Plan p) { return p.Id == plan.Id; })] = plan;
                    break;
                case BusinessEntity.States.Deleted:
                    this.Delete(plan);
                    break;
            }
            plan.State = BusinessEntity.States.Unmodified;

        }


    }
}
